<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class setting extends Admin_My_Controller 
{
    public function __construct() 
    {
        parent::__construct();
    }

    public function audit_trail() 
    {
        $data = array();
        
        $data['breadcrumb'] = $this->breadCrumb(true);
        $data['buttonExport'] = $this->exportLastQuery();
        
        $arr_filter = array(
            array('Menu', 'menu', 'text'),
            array('Urutan', 'urutan', 'text'),
            array('Active', 'is_active', 'text'),
            array('Menu Induk', 'id_menu_induk', 'text'),
            array('Menu', 'id_menu', 'text'),
        );

        $data = $this->setFilterString($arr_filter);
        $data['where'] = ( '' == $data['where'] ? $data['where'] : " AND " . $data['where'] );
        $data['where'] .= " order by id_menu_induk,urutan desc";
        $data = $this->mt_menu->searchPaging(' Select * from mt_menu where 1 = 1 ' . $data['where'], '', $this->offset, 10, $this->nextPage, 5, $this->colPage);
        
        $this->log_event = "Membuka Menu Audit Trail";
        
        $this->loadMenu('/setting/mt_menu', $data);
    }
    
    public function gangguan_produk_error($kode) 
    {
        $this->load->model('/setting/gangguan_produk_model');

        $kode = urldecode($kode);

        $set_aktif = $this->input->get("status") == "0" ? "1" : "0";
        $id_biller = intval($this->input->get("id_biller"));
        $info = $this->input->get("status") == "0" ? "Menutup Layanan" : "Membuka Layanan";

        $this->gangguan_produk_model->update(array("is_gangguan" => $set_aktif), "id_produk = '$kode'", "mt_produk");
        $this->gangguan_produk_model->update(array("is_gangguan" => $set_aktif), "id_biller = '$id_biller'", "mt_biller");

        $this->log_event = "$info Produk dengan ID Produk $kode";
        
        echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
    }

    public function gangguan_produk_aktif($kode) 
    {
        $this->load->model('/setting/gangguan_produk_model');

        $kode = urldecode($kode);

        $set_aktif = $this->input->get("status") == "0" ? "1" : "0";
        $info = $this->input->get("status") == "0" ? "Mengaktifkan" : "Menonaktifkan";
        
        $this->gangguan_produk_model->update(array("is_active" => $set_aktif), "id_produk = '$kode'", "mt_produk");

        $this->log_event = "$info Produk dengan ID Produk $kode";
        
        echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
    }
    
    public function menus($no_expand = ""){
 
        $this->load->model('setting/menus');
        
        $data = array();
                     
        $data["no_expand"] = $no_expand;
        $data["result"] = $this->menus->getMenu();
        $data['breadcrumb'] = $this->breadCrumb(true);
                        
        $this->log_event = "Membuka Master Menu";
        
        $this->loadMenu('/setting/menus', $data);
    }
    
    public function menus_edit($parent, $arr_menu = "", $kode = ""){

        $this->load->model('setting/menus');
        
        $data = array();
        $data["kode"] = $kode;
        $row = $this->menus->getMenuDetail($parent);
        $data["parent_name"] = $row->menu;
        $data["super_parent"] = $row->id_menu_induk;
        
        $row = $this->menus->getFirstLast($parent);
        $data["first"] = $row->first ? $row->first : 1;
        $data["last"] = $row->last ? $row->last :  0;
        
        if($this->input->get_post("btnSimpan")){
            $urutan_opt = $this->input->get_post("urutan_opt");
            if($urutan_opt == "first"){
                $urutan = $data["first"];
                $this->menus->shiftMenu($parent, $urutan);
            }
            else if($urutan_opt == "last")
                $urutan = $data["last"] + 1;
            else{
                $urutan = $this->input->get_post("urutan");
                if($urutan_opt == "after")
                    $urutan++;
                $this->menus->shiftMenu($parent, $urutan);
            }
            
            $arrVals = array(
                "id_menu_induk" => $parent,
                "menu"          => $this->input->get_post("menu"),
                "link"          => $this->input->get_post("link"),
                "urutan"        => $urutan,
                "is_active"     => $this->input->get_post("is_active"),
                "use_db_replika"=> $this->input->get_post("use_db_replika"),
                "is_sub_menu"   => $data["super_parent"] ? 1 : NULL
            );
            
            if($kode != "")
                $this->menus->updateMenu($kode, $arrVals);
            else 
                $this->menus->addMenu($arrVals);
            
            echo "<script>" . CloseGB(true) . "</scr" . "ipt>";;
        }
        else{
            if($arr_menu != ""){
                $arr_menu = explode("%2C", $arr_menu);
                if(is_array($arr_menu))
                    $this->menus->updateUrutan($arr_menu);
            }
            
            if($kode != ""){
                $data["row"] = $this->menus->getMenuDetail($kode);
                $urutan = $data["row"]->urutan - 1;
            }
            else
                $urutan = $data["last"];
            
            $daftar_menu = $this->menus->getMenu($parent, $kode);
            $data["daftar_menu"] = Generic::createCombo($daftar_menu->result(), $daftar_menu->num_rows(), "urutan", "menu", "urutan", $urutan, "", "style=\"width: 325px\"");
        }
        
        $this->loadMenuHeaderPopup('/setting/menus_add', $data);
    }
    
    public function menus_setAktif($kode, $flag){
     
        $this->load->model('setting/menus');
        
        $kode = str_replace("%20", " ", $kode);
        
        if($kode != null)
        {
            $info = $flag ? "Mengaktifkan" : "Menonaktifkan";
            
            $arrVals = array(
                "is_active" => $flag
            );

            $this->menus->updateMenu($kode, $arrVals);

            $this->log_event = $info . " Menu dengan ID Menu " . $kode;
            
            header("location:" . base_url() . 'index.php/setting/menus');
        }
    }
    
    public function menus_setReplika($kode, $flag){
    
        $this->load->model('setting/menus');
        
        $kode = str_replace("%20", " ", $kode);
        
        if($kode != null)
        {
            $info = $flag ? "Mengaktifkan" : "Menonaktifkan";
            
            $arrVals = array(
                "use_db_replika" => $flag
            );

            $this->menus->updateMenu($kode, $arrVals);

            $this->log_event = $info . " Penggunaan DB Replika Menu dengan ID Menu " . $kode;
         
            header("location:" . base_url() . 'index.php/setting/menus');
        }
    }
    
    public function menus_shift($arr_menu, $param){
    
        $this->load->model('setting/menus');
        
        if($arr_menu != ""){
            $arr_menu = explode("%2C", $arr_menu);
            if(is_array($arr_menu))
                $this->menus->updateUrutan($arr_menu);
        }
        
        $param_arr = explode("_", $param);
        
        if($param_arr[0] != "" && $param_arr[1] != "" && $param_arr[2] != "" && $param_arr[3] != ""){
            $this->menus->updateMenu($param_arr[0], array("urutan" => $param_arr[3]));
            $this->menus->updateMenu($param_arr[2], array("urutan" => $param_arr[1]));
        }
        
        $this->log_event = " menukar posisi menu dengan ID Menu " . $param_arr[0] . " dan " . $param_arr[2];
        
        header("location:" . base_url() . "index.php/setting/menus/".$param_arr[4]);
    }
    
    
}
